<?php
/**
 * Date: 2024.8
 * Author: kahn
 */
namespace  service\uc;

use Log;
use Service;
use AlibabaCloud\SDK\Dypnsapi\V20170525\Dypnsapi;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dypnsapi\V20170525\Models\GetMobileRequest;

class MobileAuthService extends Service {

    private static function createClient($accessKeyId, $accessKeySecret, $host): Dypnsapi
    {
        $config = new Config([
            "accessKeyId" => $accessKeyId,
            "accessKeySecret" => $accessKeySecret,
            "endpoint" => $host,
        ]);

        return new Dypnsapi($config);
    }

    public static function getMobile($accessToken): ?string
    {

        $config=\Yaf_Application::app()->getConfig()['mobile_auth']['aliyun'];
        try{
            $client = self::createClient($config['accessKeyId'], $config['accessSecret'], $config['host']);
            $options = ['accessToken'=>$accessToken, 'outId'=>'mobile_auth_' . rand(11111,99999)];
            $getMobileRequest = new GetMobileRequest($options);
            $res = $client->getMobile($getMobileRequest);
            $res1 = $res->toMap()['body'];
            if(!$res || !$res1 || $res['Code'] != 'OK'){
                Log::error("本机号码认证登录请求失败 ", $res);
                return null;
            }
            return trim($res['GetMobileResultDTO']['Mobile']);
        }catch (\Exception $e){
            Log::error("本机号码认证登录异常 ". $e->getMessage());
            return null;
        }
    }


}